package com.ruoyi.web.controller.purchaseplan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.MaterialsRequisitiones;
import com.ruoyi.domain.TotalInventory;
import com.ruoyi.service.IMaterialsRequisitionesService;
import com.ruoyi.service.ITotalInventoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/ruoyi/purchaseplan")
public class purchaseplanController extends BaseController {
    private String prefix = "ruoyi/purchaseplan";
    @Autowired
    private IMaterialsRequisitionesService materialsRequisitionesService;
    @Autowired
    private ITotalInventoryService totalInventoryService;
    @RequiresPermissions("ruoyi:purchaseplan:view")
    @GetMapping()
    public String requisitiones()
    {
        return prefix + "/purchaseplan";
    }

    /**
     * 查询总项目对应的材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long mId)
    {
        startPage();
        List<MaterialsRequisitiones> list = materialsRequisitionesService.selectLists(mId,4L);
        return getDataTable(list);
    }

    @GetMapping("/locals/{matDetailss}")
    public String local(@PathVariable("matDetailss") String matDetailss, ModelMap map)
    {
        map.put("matDetailss", matDetailss);
        return prefix + "/local";
    }

    /**
     * 查询对应的总库存材料
     */
    @RequiresPermissions("ruoyi:requisitiones:locals")
    @PostMapping("/locals")
    @ResponseBody
    public TableDataInfo locals(String tName)
    {
        TotalInventory totalInventory =new TotalInventory();
        totalInventory.settName(tName);
        return getDataTable(totalInventoryService.selectTotalInventoryList(totalInventory));
    }

    @GetMapping("/price/{mMid}")
    public String add(@PathVariable("mMid") Long mMid, ModelMap map)
    {
        MaterialsRequisitiones materialsRequisitiones = materialsRequisitionesService.selectMaterialsRequisitionesById(mMid);
        map.put("materialsRequisitiones", materialsRequisitiones);
        return prefix + "/prices";
    }


    /**
     * 修改保存材料申请单
     */
    @RequiresPermissions("ruoyi:requisitiones:edit")
    @Log(title = "材料申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialsRequisitiones materialsRequisitiones)
    {
        MaterialsRequisitiones materialsRequisitiones1 = materialsRequisitionesService.selectMaterialsRequisitionesById(materialsRequisitiones.getmMid());
        TotalInventory totalInventory=totalInventoryService.selecttname(materialsRequisitiones1.getMatDetailss());
        TotalInventory totalInventory1=new TotalInventory();
        //计算输入的已购数量和数据库中已购数量的和
        Long mSum = materialsRequisitiones.getmBuy()+materialsRequisitiones1.getmBuy();
        //计算输入的取库数量和数据库中取库的数量的和
        Long mSumm = materialsRequisitiones1.getmLocal()+materialsRequisitiones.getmLocal();
        //判断  如果取库总和大于材料数则回滚
        if(mSum>materialsRequisitiones1.getMatNumbers()){
            return toAjax(0);
        //判断  如果取库总和大于库存材料总数则回滚
        }else if(materialsRequisitiones.getmLocal()>totalInventory.gettNum()) {
            return toAjax(0);
        //判断  如果所有数值的和大于材料总数则回滚
        }else if(mSumm+mSum>materialsRequisitiones1.getMatNumbers()){
            return toAjax(0);
        }else{
            totalInventory1.setId(totalInventory.getId());
            totalInventory1.settNum(totalInventory.gettNum() - materialsRequisitiones.getmLocal());
            totalInventoryService.updateTotalInventory(totalInventory1);
            long  unb = materialsRequisitiones.getmLocal()+materialsRequisitiones.getmBuy();
            materialsRequisitiones.setmBuy(mSum);
            materialsRequisitiones.setmLocal(mSumm);
            materialsRequisitiones.setmUnbuy(materialsRequisitiones1.getmUnbuy()-unb);
            System.out.println(materialsRequisitiones1.getmUnbuy());
            materialsRequisitionesService.updateMaterialsRequisitiones(materialsRequisitiones);
        }
        MaterialsRequisitiones materialsRequisitiones2 = materialsRequisitionesService.selectMaterialsRequisitionesById(materialsRequisitiones.getmMid());
        //判断  数据库中未购买数量小于0的回滚
        if(materialsRequisitiones2.getmUnbuy()<0){
            return toAjax(0);
        //判断  数据库中的未购买数量为0则修改状态为1(1:已完成,0:未完成)
        }else if (materialsRequisitiones2.getmUnbuy()==0) {
            materialsRequisitiones.setmIsover(1L);
           materialsRequisitionesService.updateMaterialsRequisitiones(materialsRequisitiones);
        }
        return toAjax(materialsRequisitionesService.updateMaterialsRequisitiones(materialsRequisitiones));

    }
}

